package org.clockworkmages.games.anno1186.gui;

import java.awt.Graphics;
import java.awt.Image;

import javax.swing.JComponent;

public class AnimatedCircularImage
		extends AnimatedImage {

	/**
	 * Location of the center as a 0-100% of the panel's width.
	 */
	private double centerX;
	/**
	 * Location of the center as a 0-100% of the panel's height.
	 */
	private double centerY;
	/**
	 * Location of the center as a 0-100% of the panel's width or height
	 * (depending on whether radiusIsX=true).
	 */
	private double radius;
	private boolean radiusIsX;

	public AnimatedCircularImage(Image image, double centerX, double centerY,
			double radius, boolean radiusIsX) {
		super(image);
		this.centerX = centerX;
		this.centerY = centerY;
		this.radius = radius;
		this.radiusIsX = radiusIsX;
	}

	@Override
	public void draw(Graphics g, JComponent panel) {
		double radiusInPixels;
		if (radiusIsX) {
			radiusInPixels = panel.getWidth() * this.radius;
		} else {
			radiusInPixels = panel.getHeight() * this.radius;
		}

		double x = centerX * panel.getWidth()
				+ Math.sin(2 * Math.PI * (1 - getState())) * radiusInPixels
				// center the image at the selected point
				- getImage().getWidth(null) / 2;
		double y = centerY * panel.getHeight()
				+ Math.cos(2 * Math.PI * (1 - getState())) * radiusInPixels
				// center the image at the selected point
				- getImage().getHeight(null) / 2;
		;

		g.drawImage(getImage(), (int) x, (int) y, null);
	}

}
